package com.Policing.uhf.common;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.Policing.R;
import com.Policing.misc.Accompaniment;
import com.Policing.uhf.util.DataTransfer;
import com.senter.iot.support.openapi.uhf.UhfD2;

import java.util.ArrayList;
import java.util.List;


public abstract class Activity0InventoryCommonAbstract extends Activity {
    public static final String TAG = "Activity0CmnAbstract";
    private final Accompaniment accompaniment = Accompaniment.newInstanceOfResource(Activity0InventoryCommonAbstract.this, R.raw.tag_inventoried);
    //播放声音
    private List<String> list = new ArrayList<>();
    private Handler accompainimentsHandler;
    
    private final Runnable accompainimentRunnable = new Runnable() {
        @Override
        public void run() {
            accompaniment.start();
            accompainimentsHandler.removeCallbacks(this);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity20inventory);
        UhfD2.getInstance().init();
//        if (UhfD2.getInstance().init())
    //    {
    //        Log.i("jjjjjjjjj","inininin");
    //    }else Log.i("jjjjjjjjj","notnotnot");


        UhfD2.getInstance().iso18k6cSetAccessEpcMatch(UhfD2.UmdEpcMatchSetting.newInstanceOfDisable());
        HandlerThread htHandlerThread = new HandlerThread("");
        htHandlerThread.start();
        accompainimentsHandler = new Handler(htHandlerThread.getLooper());
        views = new Views();
    }
    
    @Override
    protected void onDestroy() {
        if (accompainimentsHandler != null) {
            accompainimentsHandler.getLooper().quit();
        }
        accompaniment.release();
        super.onDestroy();
    }
    
    /**
     * 显示扫描信息
     */
    
    protected final void addNewUiiMassageToListview(UhfD2.UII uii) {
        if (!list.contains(DataTransfer.xGetString(uii.getBytes()).replace(" ","").substring(4)))
            list.add(DataTransfer.xGetString(uii.getBytes()).replace(" ","").substring(4));
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getViews().editText.setText(list.toString());
            }
        });
        trigTagAccompainiment();//播放声音
        Log.i("222222222", DataTransfer.xGetString(uii.getBytes()));
        //views.recordsBoard.addMassage("Uii:" + DataTransfer.xGetString(uii.getBytes()));
        }


    
    protected abstract void uiOnInverntryButton();
    
    private void trigTagAccompainiment() {
        accompainimentsHandler.post(accompainimentRunnable);
    }

    
    private Views views;
    
    protected final Views getViews() {
        return views;
    }
    
    protected final class Views {
        private final Button save_data = (Button) findViewById(R.id.save);
        private final TextView editText = (TextView) findViewById(R.id.ed_ww);
        private final Button btnInventoryOrStop = (Button) findViewById(R.id.idE20InventoryMain_llInvestory_btnStart);
     //   private final RecordsBoard recordsBoard = new RecordsBoard(Activity0InventoryCommonAbstract.this, findViewById(R.id.idE20InventoryActivity_inShow));
       // private final RadioGroup rGroup = (RadioGroup) findViewById(R.id.idE20InventoryMain_llInvestory_rgRbs);
        private final View.OnClickListener mBtnInventoryClickLisener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uiOnInverntryButton();
            }
        };

        public Views() {
            save_data.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(Activity0InventoryCommonAbstract.this,list.toString(),Toast.LENGTH_SHORT).show();
                }
            });
           // rGroup.check(R.id.idE20InventoryMain_llInvestory_rbSingleStep);
            btnInventoryOrStop.setOnClickListener(mBtnInventoryClickLisener);
        }
        public final void setStateStoped() {
            runOnUiThread(new Runnable() {
                
                @Override
                public void run() {
                   // views.rGroup.setEnabled(true);
                    views.btnInventoryOrStop.setText(R.string.Inventory);
                }
            });
        }

        
        public final void setStateStarted() {
            runOnUiThread(new Runnable() {
                
                @Override
                public void run() {
                   // views.rGroup.setEnabled(false);
                    views.btnInventoryOrStop.setText(R.string.Stop);
                }
            });
        }

    }

}
